package cc.ding.servlet;

import cc.ding.entity.Employee;
import cc.ding.service.EmployeeService;
import cc.ding.service.Impl.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "loginServlet",urlPatterns = "/login.action")
public class LoginServlet extends HttpServlet {
    EmployeeService employeeService = new EmployeeServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收前台参数
        String employeeNo = req.getParameter("employeeNo");
        String password = req.getParameter("password");

        Employee employee = employeeService.login(employeeNo,password);

        if (employee!=null){
            //将员工信息存放在session
            HttpSession session = req.getSession();
            session.setAttribute("employee",employee);
            resp.sendRedirect("main.jsp");
        }

    }
}
